package mspbots.autotask.client.core.data.companyalerts;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.log4j.Log4j2;
import mspbots.autotask.client.core.AbstractToolsUtil;
import mspbots.autotask.client.core.BaseApiService;
import mspbots.autotask.client.core.client.ClientProperties;
import mspbots.autotask.client.core.client.ClientUtils;
import mspbots.autotask.common.*;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.net.URI;
import java.time.LocalDateTime;
import java.util.UUID;

@Log4j2
@Service
public class CompanyAlertsServiceImpl extends AbstractToolsUtil implements BaseApiService {
    private final String QUERY_REQUEST_PATH = "/V1.0/CompanyAlerts/query";

    @Override
    public Mono<JsonNode> find(Long tenantId, QueryBody queryBody, URI pageUrl) {
        return null;
    }

    @Override
    public Mono<JsonNode> count(Long tenantId, QueryBody queryBody) {
        return null;
    }

//    @Override
//    public Mono<Void> syncSlow(ClientProperties properties) {
//        return syncData8(properties).then();
//    }

    public Mono<Void> syncData8(ClientProperties properties) {
        ResultSync resultSync = ResultSync.withDefault("COMPANY-ALERTS-SYNC");
        return this.dateCacheFilterGetUsers(properties)
                .subscriberContext(ctx -> ctx.put("resultSync", resultSync))
                .doOnNext(result -> this.syncResult(properties.getTenantId(), resultSync.getRequestId(), result)).then();
    }

//    @Override
//    public Mono<Void> syncInformation(ClientProperties properties) {
//        ResultSync resultInfoSync = ResultSync.withDefault("COMPANY-ALERTS-ENTITY-INFORMATION");
//        return this.entityInformation(properties)
//                .subscriberContext(ctx -> ctx.put("resultSync", resultInfoSync))
//                .doOnNext(result -> this.syncInformationResult(properties.getTenantId(),
//                        resultInfoSync.getRequestId(), result))
//                .then();
//    }

    private void syncResult(Long tenantId, UUID id, JsonNode resultNode) {
        this.sendEntity(RabbitKeys.COMPANY_ALERTS_SYNC_QUEUE_KEY, ResultMsg.builder()
                .requestId(id).id(resultNode.get("id").longValue())
                .action("none").entity(resultNode).tenantId(tenantId)
                .syncDate(LocalDateTime.now()).build());
    }

    private Flux<JsonNode> dateCacheFilterGetUsers(ClientProperties properties) {
        return this.fetch(properties, QueryBody.withFilter(FilterExpression.builder()
                .op("gt").field("id").value("0").build()));
    }

    private Flux<JsonNode> fetch(ClientProperties properties, QueryBody queryBody) {
        return super.autoTaskClient
                .fetch(ClientUtils.builderRequestUrl(properties.getBaseUrl(), QUERY_REQUEST_PATH), queryBody)
                .subscriberContext(ctx -> ctx.put("properties", properties));
    }

    private void syncInformationResult(Long tenantId, UUID id, JsonNode resultNode) {
        this.sendEntity(RabbitKeys.INFORMATION_SYNC_QUEUE_KEY, ResultMsg.builder()
                .requestId(id).action("CompanyAlerts")
                .entity(resultNode).tenantId(tenantId)
                .syncDate(LocalDateTime.now()).build());
    }

    private Flux<JsonNode> entityInformation(ClientProperties properties) {
        return super.autoTaskClient.get(ClientUtils.builderRequestUrl(properties.getBaseUrl(),
                "/V1.0/CompanyAlerts/entityInformation/fields"))
                .subscriberContext(ctx -> ctx.put("properties", properties))
                .flatMapMany(jsonNode -> Flux.fromIterable(jsonNode.withArray("fields")));
    }
}
